package day02;

import java.sql.SQLOutput;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/2/25 21:24
 */
public class BinarySearchBalance {
    public static void main(String[] args) {
        int[] arr = { 1, 2, 4, 5, 7, 9, 11 };
        int index = binarySearchBalance(arr, 5);
        System.out.println(index);
    }


    /**
     * 平均比较次数减少了!!!
     * @param arr
     * @param target
     * @return
     */
    public static int binarySearchBalance(int[] arr, int target) {
        int left = 0;
        int right = arr.length;
        while (right - left > 1) {
            int center = (left + right) >>> 1;
            if (target < arr[center]) {
                right = center;
            } else {
                left = center;
            }
        }
        return target == arr[left] ? left : -1;
    }
}
